import re
from PySide6.QtCore import Qt, QRegularExpression
from PySide6.QtGui import QRegularExpressionValidator
from PySide6.QtWidgets import  QWidget, QHBoxLayout, QVBoxLayout
from qfluentwidgets import SingleDirectionScrollArea, SimpleCardWidget, CaptionLabel, LineEdit, PushButton, CheckBox
from core.util import  loadini_setting, dumpini_setting
import setting as c
from core.log import logger

class SettingInfoCard(SimpleCardWidget):
    """ App information card """

    def __init__(self, parent=None):
        super().__init__(parent)
       
        regex = QRegularExpression(c.exp_ip)
        validator = QRegularExpressionValidator(regex)
        layout = QHBoxLayout()
        self.ip_label = CaptionLabel('ip: ', self)
        self.ip_le = LineEdit(self)
        self.ip_le.setValidator(validator)
        
        regex = QRegularExpression(c.exp_port)
        validator = QRegularExpressionValidator(regex)
        self.port_label = CaptionLabel('port: ', self)
        self.port_le = LineEdit(self)
        self.port_le.setValidator(validator)
        
        self.ok_btn = PushButton('保存', self)
        self.ok_btn.clicked.connect(self.onclick_ok)
        
        self.tips_label = CaptionLabel('', self)
        
        self.devmode_cb = CheckBox('开发者模式')
        self.devmode_cb.stateChanged.connect(self.onStateChanged_devmode)

        self.vBoxLayout = QVBoxLayout(self)
        self.hBoxLayout_ip = QHBoxLayout()
        self.hBoxLayout_port = QHBoxLayout()
        self.hBoxLayout_ok = QHBoxLayout()
        self.vBoxLayout_tips = QVBoxLayout()
        
        self.initLayout()

    def initLayout(self):
        self.hBoxLayout_ip.setSpacing(30)
        self.hBoxLayout_ip.setContentsMargins(34, 24, 24, 24)
        self.hBoxLayout_ip.addWidget(self.ip_label, 1)
        self.hBoxLayout_ip.addWidget(self.ip_le, 4)
        
        self.hBoxLayout_port.setSpacing(30)
        self.hBoxLayout_port.setContentsMargins(34, 24, 24, 24)
        self.hBoxLayout_port.addWidget(self.port_label, 1)
        self.hBoxLayout_port.addWidget(self.port_le, 4)
        
        self.hBoxLayout_ok.setSpacing(30)
        self.hBoxLayout_ok.setContentsMargins(34, 24, 24, 24)
        self.hBoxLayout_ok.addWidget(self.ok_btn)
        
        self.vBoxLayout_tips.setSpacing(30)
        self.vBoxLayout_tips.setContentsMargins(34, 24, 24, 24)
        self.vBoxLayout_tips.addWidget(self.devmode_cb)
        self.vBoxLayout_tips.addWidget(self.tips_label)
        
        self.vBoxLayout.addLayout(self.hBoxLayout_ip)
        self.vBoxLayout.addLayout(self.hBoxLayout_port)
        self.vBoxLayout.addLayout(self.hBoxLayout_ok)
        self.vBoxLayout.addLayout(self.vBoxLayout_tips)
        
    def onclick_ok(self):
        ip_new = self.ip_le.text()
        port_new = self.port_le.text()
        if not self.is_valid_ip(ip_new):
            self.setTips("Ip 格式错误")
            return
        if not self.is_valid_port(port_new):
            self.setTips("Port 格式错误")
            return
        dumpini_setting(ip_new, port_new, c.VERSION)
        c.URL_QA = f"http://{ip_new}:{port_new}"
        logger.info(f"modify setting ini {c.URL_QA}")
        self.setTips("保存成功")
        
    def onStateChanged_devmode(self, state):
        if state == 2:  # 选中状态
            logger.info("developer mode confirm")
            c.develop_mode = True
        else:  # 未选中状态
            logger.info("developer mode cancel")
            c.develop_mode = False
        
    def setTips(self, tips):
        self.tips_label.setText(f"<font color='red'>{tips}</font>")
        
    def is_valid_ip(self, port):
        return re.match(c.exp_ip, port) is not None     
    
    def is_valid_port(self, port):
        return re.match(c.exp_port, port) is not None 
    
    def show(self):
        res, ok = loadini_setting()
        if ok:
            self.ip_le.setText(res[0])
            self.port_le.setText(res[1])

        


class SettingInterface(SingleDirectionScrollArea):

    def __init__(self, parent=None):
        super().__init__(parent=None)

        self.view = QWidget(self)

        self.vBoxLayout = QVBoxLayout(self.view)
        self.settingCard = SettingInfoCard(self)
        self.settingCard.setMaximumWidth(c.card_width)
        self.setWidget(self.view)
        self.setWidgetResizable(True)
        self.setObjectName('SettingInterface')

        self.vBoxLayout.setSpacing(10)
        self.vBoxLayout.setContentsMargins(0, 0, 15, 0)
        self.vBoxLayout.addWidget(self.settingCard, 0, Qt.AlignTop)

        self.setStyleSheet("QScrollArea {border: none; background:transparent}")
        self.view.setStyleSheet('QWidget {background:transparent}')
        
    def show(self):
        print("show SettingInterface")
        self.settingCard.show()